var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var base64 = require("../../example/images/base64");

Page({
    data: {
        itemid:'',
        status:'',
        itemdesc:'',
        url:'',
        itemname:'',
        tabs: ["借/还", "借还记录"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        qr:'',
        loglist:[]
    },
    onLoad: function (options) {
      var that = this
      /**** */
      var that = this
      /*wx.connectSocket({
        url: 'ws://192.168.10.195/ws?user_id=' + getApp().globalData.userid
      })*/
      wx.onSocketMessage(function (res) {
        var rt = JSON.parse(res.data)
        console.log(JSON.stringify(rt))
        console.log('收到服务器内容：' + rt.toid)
        if(rt.cmd=="borrow"){
          var toid = rt.myid
          var itemid = rt.itemid
          wx.showModal({
            title: '提示',
            content: '是否同意借出此物品',
            success: function (res) {
              if (res.confirm) {
                //console.log('同意')
                wx.sendSocketMessage({
                  data: JSON.stringify({itemid:itemid, myid: getApp().globalData.userid, toid: toid, cmd: 'ok' })
                })
              } else if (res.cancel) {
                //console.log('拒绝')
                wx.sendSocketMessage({
                  data: JSON.stringify({ itemid: itemid,myid: getApp().globalData.userid, toid: toid, cmd: 'refuse' })
                })
              }
            }
          })
        }
      })
      /**** */
      showView: (options.showView == "true" ? true : false)
      this.setData({
        itemid: options.itemid
      }) 
      wx.request({
        url: getApp().globalData.serverip + '/item?itemid=' + options.itemid,
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'sessionid': wx.getStorageSync("sessionid")
        },
        success: function (res) {
          console.log(JSON.stringify(res))
          that.setData({
            status: res.data.itemlist[0].status,
            itemdesc: res.data.itemlist[0].itemdesc,
            url: res.data.itemlist[0].url,
            itemname: res.data.itemlist[0].itemname,
            qr: getApp().globalData.serverip+"/qr/"+res.data.itemlist[0].id+".png"
          })
        }
      })

      wx.request({
        url: getApp().globalData.serverip + '/log?itemid=' + options.itemid,
        method: 'POST',
        header: {
          'content-type': 'application/json', // 默认值
          'sessionid': wx.getStorageSync("sessionid")
        },
        success: function (res) {
          console.log(JSON.stringify(res))
          that.setData({
            loglist: res.data.loglist
          })
          
          /*that.setData({
            status: res.data.itemlist[0].status,
            itemdesc: res.data.itemlist[0].itemdesc,
            url: res.data.itemlist[0].url,
            itemname: res.data.itemlist[0].itemname,
            qr: getApp().globalData.serverip + "/qr/" + res.data.itemlist[0].id + ".png"
          })*/
        }
      })

      
    },
    tabClick: function (e) {
      this.setData({
        sliderOffset: e.currentTarget.offsetLeft,
        activeIndex: e.currentTarget.id
      });
    }
});